<?php

include_once($GLOBALS['cfgPaths']['lib'].'model2/Filter.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/ActionForm.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/WebAction.o.inc.php');
include_once($GLOBALS['cfgPaths']['lib'].'model2/SynchronizerToken.o.inc.php');


class ActionDispatcherFilter extends Filter
{

    var $actiondict;
    var $defaulterroraction;
    var $defaultaction;
    var $fileending;
    var $basedir;

    function init (& $config)
    {
        $this->actiondict = $config['actiondict'];
        $this->defaulterroraction = $config['defaulterroraction'];
        $this->defaultaction = $config['defaultaction'];
        $this->fileending = $config['classfileending'];
        $this->basedir = $config['basedir'];
    }
    
    function doFilter (& $env, & $chain) 
    {
        $this->dispatch($env);
        $chain->doFilter($env);
    }
    
    function dispatch(& $env)
    {
        if ($action = $env->getAttribute('__actionParam__'))
        {
            if (empty($this->actiondict[$action]))
            {
                $this->doDispatch($this->defaulterroraction, $env);
            }
            else
            {
                $this->doDispatch($this->actiondict[$action], $env);
            }
        }
        else
        {
            $this->doDispatch($this->defaultaction, $env);
        }
    }
    
    function doDispatch ($actionclass, & $env)
    {
        if (file_exists($this->basedir . '/action/'.$actionclass['file'].$this->fileending))
        {
            include_once($this->basedir . '/action/'.$actionclass['file'].$this->fileending);
            $actionobj = new $actionclass['class'](); 
            $actionobj->executeAction($this, $env, $this->basedir);
        }
    }

}

?>